package com.tcl.aota.manage.imp;

import com.tcl.aota.cache.AppCacheManage;
import com.tcl.aota.cache.redis.utils.Constant;
import com.tcl.aota.common.constants.Constants;
import com.tcl.aota.common.utils.DateUtil;
import com.tcl.aota.manage.PackageManage;
import com.tcl.aota.persistent.dao.db.AppDAO;
import com.tcl.aota.persistent.dao.db.PackageAppDAO;
import com.tcl.aota.persistent.dao.db.PackageDAO;
import com.tcl.aota.persistent.model.App;
import com.tcl.aota.persistent.model.Package;
import com.tcl.aota.persistent.model.PackageApp;
import org.perf4j.aop.Profiled;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("packageManage")
public class PackageManageImp implements PackageManage {

    private static final Logger LOG = LoggerFactory
        .getLogger(PackageManageImp.class);

    @Resource(name = "packageDAO")
    private PackageDAO packageDAO;

    @Resource(name = "appDAO")
    private AppDAO appDAO;

    @Resource
    private AppCacheManage appCacheManage;

    @Resource(name = "packageAppDAO")
    private PackageAppDAO packageAppDAO;

    @Override
    @Profiled
    public List<Package> queryByCondition(Map<String, Object> condition) {
        return packageDAO.selectPackages(condition);
    }

    @Override
    public int countByCondition(Map<String, Object> condition) {
        return packageDAO.countPackages(condition);
    }

    @Override
    @Profiled
    public String release(long id) {
        String result = null;
        Date releaseTime = new Date();
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("id", id);
        param.put("releaseTime", releaseTime);
        if (packageDAO.releasePackages(param) > 0) {
            List<App> latestPackageApps = appDAO.selectAppListByPackageId(id);
            result = DateUtil.fomartDateToStr("yyyy-MM-dd HH:mm:ss", releaseTime);
            //app的release_status改未release
            //appDAO.releaseApp(id);
        }
        return result;
    }

    @Override
    @Profiled
    public int dismiss(List<Long> ids) {
        int result = dismissPackage(ids);
        if (result > 0) {
            //update cache
            Package latestPackage = packageDAO.selectLatestPackage();
            List<App> latestPackageApps=null;
            if(latestPackage!=null){
                latestPackageApps = appDAO.selectAppListByPackageId(latestPackage.getId());
            }
        }
        return result;
    }

    @Override
    @Profiled
    @Transactional
    public int dismissPackage(List<Long> ids) {
        int result = Constants.Common.SUCCESS;
        try {
            packageDAO.deleteByPrimaryKeys(ids);
            packageAppDAO.deleteBydeleteByPackageIds(ids);
            //修改未在release package的app的status为not release
            appDAO.recoverAppReleaseStatus();
        } catch (Exception e) {
            result = Constants.Common.FAIL;
            LOG.error(e.getMessage(), e);
        }
        return result;
    }

    @Override
    @Profiled
    @Transactional(propagation = Propagation.REQUIRED)
    public int putAppToPackage(List<Long> ids) {
        int result = Constants.Common.FAIL;
        try {
            List<App> appList = appDAO.selectAppListByIds(ids);
            int appCount = (appList == null) ? 0 : appList.size();
            float packageSize = 0;
            for (App app : appList) {
                packageSize += Long.parseLong(app.getAppSize());
            }
            Package pck = new Package();
            pck.setAppQuantity(appCount);
            pck.setPackageSize(packageSize);
            String packageName = Constants.Common.PACKAGE_PREFIX
                + DateUtil.fomartDateToStr("yyyyMMddHHmmss", new Date());
            pck.setPackageName(packageName);
            pck.setCreateTime(new Date());
            pck.setPackageSequence(Constants.Common.DEFAULT_SEQUENCE);
            packageDAO.insert(pck);
            //插入package_app关联表
            for (Long id : ids) {
                PackageApp packageApp = new PackageApp();
                packageApp.setAppId(id);
                packageApp.setPackageId(pck.getId());
                result += packageAppDAO.insert(packageApp);
            }
        } catch (NumberFormatException e) {
            LOG.error(e.getMessage(),e);
        }
        return result;
    }

    @Override
    public List<App> getAppsByPackage(long packageId) {
        return appDAO.selectAppListByPackageId(packageId);
    }
}
